#ifndef _SYNCH_PROPS_H_
#define _SYNCH_PROPS_H_

#define GUARD INT_MIN

#define PSIZE                   4096


typedef struct PoolStruct {
	void *p[PSIZE];
	int index;
	int obj_size;
} PoolStruct;

inline static void *getMemory(size_t size) {
	void *p = malloc(size);

	if (p == null) {
		perror("malloc failed");
		exit(EXIT_FAILURE);
	} else return p;
}

inline static void *getAlignedMemory(size_t align, size_t size) {
	void *p;

	p = (void *)ALIGNED_MALLOC(size,align);
	if (p == null) {
		perror("memalign");
		exit(EXIT_FAILURE);
	} else return p;
}

inline static void init_pool(PoolStruct *pool, int obj_size) {
	void *objects;
	int i;

	objects = getAlignedMemory(CACHE_LINE_SIZE, PSIZE * obj_size);
	pool->obj_size = obj_size;
	pool->index = 0;
	for (i = 0; i < PSIZE; i++)
		pool->p[i] = (void *)((char*)objects + (int)(i * obj_size));
}

inline static void *alloc_obj(PoolStruct *pool) {
	if (pool->index == PSIZE) {
		int size = pool->obj_size;
		init_pool(pool, size);
	}

	return pool->p[pool->index++];
}

inline static void free_obj(PoolStruct *pool, void *obj) {
	if (pool->index > 0)
		pool->p[--pool->index] = obj;
}

inline static void rollback(PoolStruct *pool, int num_objs) {
	if (pool->index - num_objs >= 0)
		pool->index -= num_objs;
	else
		pool->index = 0;
}

typedef struct ListNode {
	Object val;
	struct ListNode* next;
}ListNode;

ListNode CACHE_ALIGN guard_node  = {GUARD, null};
volatile ListNode CACHE_ALIGN *last = &guard_node;
volatile ListNode CACHE_ALIGN *first = &guard_node;
int64_t CACHE_ALIGN d1 , d2;


#endif
